package Chapter05;

import com.sun.org.apache.bcel.internal.generic.NEW;

import java.util.Arrays;

/**
 * 使用sort()方法排序
 * 不能直接创建接口类的对象，但是可以创建接口类型的引用;
 * 接口之间可以存在继承关系,使用extends关键字来表示该关系;
 * 当使用多个接口时,各个接口之间要使用","分隔开;
 * Created by a on 2017/6/10.
 */
public class Test115 {

    public static void main(String[] args) {
        Employee employee1 = new Employee(3,"Java",1);
        Employee employee2 = new Employee(2,"PhP",2);
        Employee employee3 = new Employee(1,"Ruby",3);
        Employee[] employees = new Employee[3];
        employees[0] = employee1;
        employees[1] = employee2;
        employees[2] = employee3;
        System.out.println("排序前:");
        for (Employee employee : employees){
            System.out.println(employee);
        }
        System.out.println("排序后:");
        Arrays.sort(employees);
        for (Employee employee : employees){
            System.out.println(employee);
        }
    }
}


class Employee implements Comparable<Employee>{

    private int id;
    private String name;
    private int age;

    public Employee(int id, String name, int age) {

        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public int compareTo(Employee o) {

        if (id>o.id){
            return 1;
        }else if (id<o.id){
            return -1;
        }
        return 0;
    }

    @Override
    public String toString() {

        StringBuilder sb = new StringBuilder();
        sb.append("员工的编号:"+id+",");
        sb.append("员工的姓名:"+name+",");
        sb.append("员工的年龄:"+age);
        return sb.toString();


    }

}